package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.*;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author CHENG KAI
 * @Date 2020/4/16 14:25
 * @Version 1.0
 */
@Transactional
@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    PermissionDao permissionDao;
    @Autowired
    RoleDao roleDao;
    @Autowired
    UserDao userDao;

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //之前：业务逻辑1.查询总记录数  2.查询当前页面显示数据
        //1.设置分页参数
        PageHelper.startPage(currentPage,pageSize);
        //2.需要分页的语句
        Page<Permission> permissionPage = permissionDao.selectByCondition(queryString);
        return new PageResult(permissionPage.getTotal(),permissionPage.getResult());
    }

    /**
     * 新增权限
     * @param permission
     * @return
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @Override
    public void deleteById(Integer id) {
//        查询删除 的权限id有没有关联的角色
        int count=permissionDao.findCountById(id);
        if(count>0){
            throw  new RuntimeException(MessageConstant.DELETE_PERMISSION_HAVE_SON);
        }
        permissionDao.deleteById(id);
    }

    /**
     *回显权限编辑窗口
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    /**
     * 编辑权限
     * @param permission
     * @return
     */
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public List<String> getPermissionByName(String username) {
//        定义一个List来接收权限关键字
        List<String> list = new ArrayList<>();
//        查询
        Integer id =userDao.getIdByName(username);
        Set<Role> roleSet = roleDao.findRolesByUserId(id);
        if (roleSet != null && roleSet.size() >0){
            for (Role role : roleSet) {
                Set<Permission> permissions = role.getPermissions();
                if (permissions != null && permissions.size() > 0){
                    for (Permission permission : permissions) {
                        String keyword = permission.getKeyword();
                        list.add(keyword);
                    }
                }
            }
        }
        return list;
    }
//     查询菜单
    @Override
    public List<String> getMenuByName(String username) {
        //        定义一个List来接收菜单名称
        List<String> list = new ArrayList<>();
//        查询菜单
        Integer id =userDao.getIdByName(username);
        Set<Role> roleSet = roleDao.findMenusByUserId(id);
        if (roleSet != null && roleSet.size() >0){
            for (Role role : roleSet) {
                LinkedHashSet<Menu> menus = role.getMenus();
                if (menus != null && menus.size()>0){
                    for (Menu menu : menus) {
                        list.add(menu.getName());
                    }
                }
            }
        }
        return list;
    }
    /**
     * 查询所有 权限信息
     */
    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
